package app;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.lidroid.xutils.util.LogUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import adapter.GroupChatAdapter;
import app.im.IMDetails2;
import app.im.details.IMDetailsActivity;
import application.HttpUrlConstant;
import interfaces.IReturnObject;
import mainUI.AppActivity;
import menhoo.com.i8officelibrary.R;
import model.IMCvnDBModel;

/**
 * Created by yanglei on 2017/3/30.
 */

public class GroupChat extends AppActivity {
    RecyclerView listView;

    ImageButton rightBtn;
    View layout;
    PopupWindow menuWindow;
    Button btnGoGroup,btnGoTalk;
    LinearLayout linearLayoutEmpty;

    List<IMCvnDBModel> list = new ArrayList<>();
    GroupChatAdapter adapter;
    WrapContentLinearLayoutManager layoutManager;

    String title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_im);
        //
        try{
            listView = (RecyclerView)findViewById(R.id.listView);
        }
        catch (Exception e){
            LogUtils.e(e.getMessage()+"");
        }

        linearLayoutEmpty=(LinearLayout)findViewById(R.id.linearLayoutEmpty);

        adapter = new GroupChatAdapter(this);
        adapter.setOnItemClickLitener(new GroupChatAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(View view, int position) {
                try{
                    IMCvnDBModel item =adapter.getItem(position);
                    Intent it =new Intent(GroupChat.this, IMDetailsActivity.class);
                    it.putExtra("Title",item.name);
                    it.putExtra("UnreadCount",item.unreadCount);
                    it.putExtra("UserCount",item.userCount);
                    it.putExtra("GroupID",item.groupID);
                    it.putExtra("Type",item.type);
                    it.putExtra("ID",item.id);
                    startActivity(it);
                }
                catch (Exception ex){}
            }

            @Override
            public void onItemLongClick(View view, int position) {
                try{


                }
                catch (Exception ex){}
            }
        });

        layoutManager =new WrapContentLinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        layoutManager.setReverseLayout(false);
        layoutManager.setSmoothScrollbarEnabled(true);
        listView.setLayoutManager(layoutManager);
        listView.getItemAnimator().setChangeDuration(0);
        listView.setAdapter(adapter);

        requestData();
    }


    /**
     * 请求会话数据
     */
    private void requestData(){
        Post(HttpUrlConstant.getWebSiteFullUrl(HttpUrlConstant.GetGroupList), new IReturnObject<String>() {
            @Override
            public void returnResult(String result) {
                if (result != null) {
                    try {

                        JSONObject jsonObject = new JSONObject(result);
                        if (jsonObject.getBoolean("Succeed")) {
                            list = new ArrayList();

                            JSONArray appArray = jsonObject.getJSONArray("Data");
                            for (int i = 0; i < appArray.length(); i++) {
                                JSONObject appJson = appArray.getJSONObject(i);
                                String ID = appJson.getString("ID");
                                String GroupType = appJson.getString("GroupType");
                                String GroupName = appJson.getString("GroupName");
                                int UserCount = appJson.getInt("UserCount");
                                int MessageCount = appJson.getInt("MessageCount");
                                String Creator = appJson.getString("Creator");
                                String CreateTime = appJson.getString("CreateTime");
                                String ImgUrl = appJson.getString("ImgUrl");

                                //
                                IMCvnDBModel model = new IMCvnDBModel();
                                model.groupID = ID;
                                model.id = ID;
                                model.type = GroupType;
                                model.userCount=UserCount;
                                model.imgUrl = ImgUrl;
                                model.name = GroupName;


                                list.add(model);
                            }
                            if(list!=null && list.size()>0){
                                linearLayoutEmpty.setVisibility(View.GONE);
                                adapter.clear();
                                adapter.add(list);
                                try {
                                    getDB().deleteAll(IMCvnDBModel.class);
                                    getDB().saveAll(list);
                                }
                                catch (Exception ex){
                                    LogUtils.e(ex.getMessage()+"");
                                }
                            }

                        }

                    } catch (Exception e) {
                        LogUtils.e(e.getMessage() + "");
                    }
                }
                showContent();
            }
        });


    }


    public class WrapContentLinearLayoutManager extends LinearLayoutManager {
        public WrapContentLinearLayoutManager(Context context) {
            super(context);
        }

        public WrapContentLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
            super(context, orientation, reverseLayout);
        }

        public WrapContentLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
        }

        @Override
        public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
            try {
                super.onLayoutChildren(recycler, state);
            } catch (IndexOutOfBoundsException e) {
                e.printStackTrace();
            }
        }
    }
}
